package com.daohang361.imqq.Asycore;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;

import com.daohang361.imqq.protocol.HexBytesProtocolfactory;

/** 
 * @author naner E-mail:likun3344@163.com 
 * @create time：2011-4-12 下午07:15:07 
 */
public class Connector {
  private  IoSession session =null;
  private  ConnectFuture future =null;
  private  NioDatagramConnector connector=null;
  /**
   * 默认远程服务器地址
   */
  public static final String defualt_ip="58.60.9.134";
  /**
   * 默认远程服务器端口
   */
  public static final int defualt_port = 8000;
  private   String server_ip=defualt_ip;
  private int server_port=defualt_port;
  public IoSession getSession() {
	return session;
  }
  public void setSession(IoSession session) {
	this.session = session;
  }
  public ConnectFuture getFuture() {
	return future;
  }
  public void setFuture(ConnectFuture future) {
	this.future = future;
  }
  public NioDatagramConnector getConnector() {
	return connector;
  }
  public void setConnector(NioDatagramConnector connector) {
	this.connector = connector;
  }
  public String getServer_ip() {
	return server_ip;
  }
  public void setServer_ip(String server_ip) {
	this.server_ip = server_ip;
  }
  public int getServer_port() {
	return server_port;
	}
  public void setServer_port(int server_port) {
	this.server_port = server_port;
	}
  
  private void defaltInit(){
	  connector= new NioDatagramConnector();
      connector.setHandler(new MinaHandler());//设置接收器的处理程序
      Executor threadPool = Executors.newFixedThreadPool(5);//建立线程池
      connector.getFilterChain().addLast("exector", new ExecutorFilter(threadPool));
      connector.getFilterChain().addLast("logging", new LoggingFilter());
      connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new HexBytesProtocolfactory(Charset.forName("UTF-8"))));
      DatagramSessionConfig dcfg = connector.getSessionConfig();//建立连接的配置文件
      dcfg.setReadBufferSize(4096);//设置接收最大字节默认2048
      dcfg.setReceiveBufferSize(1024);//设置输入缓冲区的大小
      dcfg.setSendBufferSize(1024);//设置输出缓冲区的大小
      dcfg.setReuseAddress(false);//设置每一个非主监听连接的端口可以重用
  }
  /**
   * 连接初始化或者重定位
   * @param defualtConnector
   * @return Iosession
   */
  public IoSession redirect() {
	 try {
		defaltInit();
 	   future = connector.connect(new InetSocketAddress(server_ip,server_port));
 	   future.awaitUninterruptibly();//等待连接创建完成
// 	   if( session!=null)
// 		   session.close();
 	   session = future.getSession();
 	   } catch (Exception e) {
 		   e.printStackTrace();
 	   }
 	   return session;
	}
  /**
   * 自定义创建连接
   * @param server_ip
   * @param server_port
   * @return
   */
  public IoSession CreateSession(String server_ip,int server_port) {
		 try {
			 System.out.println("new session"+server_ip+":"+server_port+"创建");
		   defaltInit();
	 	   future = connector.connect(new InetSocketAddress(server_ip,server_port));
	 	   future.awaitUninterruptibly();//等待连接创建完成
	 	   session = future.getSession();
	 	   } catch (Exception e) {
	 		   e.printStackTrace();
	 	   }
	 	   return session;
		}
	/**
 	* 关闭客户端释放资源
 	*/
	public  void close(){
	   connector.dispose();
	   }
}
